//
//  ShopModel.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/10/29.
//

#import "ShopModel.h"
#import "PhotosView.h"

@implementation ShopModel

+ (NSDictionary *)mj_replacedKeyFromPropertyName {
    return @{@"sellingPrice":@[@"sellingPrice",@"cellingPrice"]};
}


@end


@implementation ShopModelFrame

- (void)setModel:(ShopModel *)model {
    _model = model;
    [self updateFrameWithWidth:self.width];
}

- (void)updateFrameWithWidth:(CGFloat)width {
    CGFloat iconX = 16;
    CGFloat iconY = 16;
    CGFloat iconWH = 40;
    _iconF = (CGRect){{iconX, iconY}, {iconWH, iconWH}};
    
    CGFloat nameX = CGRectGetMaxX(_iconF) + 10;
    CGFloat nameY = iconY + 2;
    CGSize nameSize = [self sizeWithText:_model.nickName font:kFontMediium(14)];
    _nameF = (CGRect){{nameX, nameY}, nameSize};
    
    CGFloat timeX = nameX;
    CGFloat timeY = CGRectGetMaxY(_nameF) + 5;
    CGSize timeSize = [self sizeWithText:[NSDate formateData:_model.createdAt] font:kFontRegula(10)];
    _timeF = (CGRect){{timeX, timeY}, timeSize};
    
    CGFloat contentX = timeX;
    CGFloat contentY = CGRectGetMaxY(_timeF) + 8;
    CGFloat maxW = width - contentX - 16;
    CGSize contentSize = [self sizeWithText:_model.goodsIntroduction font:kFontRegula(13) maxW:maxW];
    _contentF = (CGRect){{contentX, contentY}, {maxW, contentSize.height}};
    
    CGFloat photosX = contentX;
    CGFloat photosY = CGRectGetMaxY(_contentF) + 10;
    CGFloat photosW = width - 64 - 16;
    CGSize photosSize = [PhotosView sizeWithImages:_model.imageModels width:photosW margin:10];
    _photosF = (CGRect){{photosX, photosY}, photosSize};
    
    CGFloat priceX = photosX;
    CGFloat priceY = CGRectGetMaxY(_photosF) + 15;
    CGSize peiceSize = [self sizeWithText:[NSString stringWithFormat:@"¥ %@",_model.sellingPrice] font:kFontMediium(16)];
    _priceF = (CGRect){{priceX, priceY}, peiceSize};
    
    CGFloat lineX = 0;
    CGFloat lineY = CGRectGetMaxY(_priceF) + 20;
    CGFloat lineW = width;
    CGFloat lineH = 0.5;
    _lineF = CGRectMake(lineX, lineY, lineW, lineH);
    
    _cellHeight = CGRectGetMaxY(_lineF);
}

- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font {
    return [text sizeWithAttributes:@{NSFontAttributeName: font}];
}

- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxW:(CGFloat)maxW {
    CGSize size = CGSizeMake(maxW, CGFLOAT_MAX);
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [text boundingRectWithSize:size options:options attributes:attrs context:nil].size;
}

@end


@implementation ShopModelImageModel

- (BOOL)isVideo {
    return self.video.length > 0;
}

@end
